using System;
using System.Data;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Profile;
using DotNetNuke.Entities.Users;
using DotNetNuke.Services.Localization;

namespace Code5Systems.FormMaster
{
	
	public partial class FormQuery : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		
#region  Web Form Designer Generated Code
		
		//This call is required by the Web Form Designer.
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		
		protected System.Web.UI.WebControls.Label lblHeading;
		protected System.Web.UI.WebControls.LinkButton cmdUpdate;
		protected System.Web.UI.WebControls.LinkButton cmdTestQuery;
		protected System.Web.UI.WebControls.TextBox txtQuery;
		protected System.Web.UI.WebControls.TextBox txtConnection;
		protected System.Web.UI.WebControls.GridView gvQueryResults;
		protected System.Web.UI.WebControls.DropDownList lstQueryTokens;
		protected System.Web.UI.WebControls.LinkButton cmdInsertQueryToken;
		
		
		//NOTE: The following placeholder declaration is required by the Web Form Designer.
		//Do not delete or move it.
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: This method call is required by the Web Form Designer
			//Do not modify it using the code editor.
			InitializeComponent();
		}
		
#endregion
		
		
#region Private Members
		
		private DotNetNuke.Entities.Modules.PortalModuleBase _loaderCtl;
		private FormSettings objFormSettings;
		private string strOperationSuccess;
		private string strInvalidQuery;
		
#endregion
		
		
#region Event Handlers
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			try
			{
				UserController objUsers = new UserController();
				UserInfo objUser = UserController.GetCurrentUserInfo();
				
				if (!(objUser.IsSuperUser || DotNetNuke.Security.PortalSecurity.IsInRole("Administrators")))
				{
					Response.Redirect("~/" + DotNetNuke.Common.Globals.glbDefaultPage);
				}
				
				this.LoaderCtl = (PortalModuleBase) this.Parent.Parent;
				
				if (File.Exists(Server.MapPath(this.ControlPath + "assets/js/InsertAtCursor.js")))
				{
					if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), this.GetType().ToString()))
					{
						StreamReader iFile = File.OpenText(Server.MapPath(this.ControlPath + "assets/js/InsertAtCursor.js"));
						Page.ClientScript.RegisterClientScriptBlock(this.GetType(), this.GetType().ToString(), iFile.ReadToEnd(), false);
						iFile.Close();
					}
					this.cmdInsertQueryToken.OnClientClick = "return C5SLLC_InsertAtCursor(\'" + this.txtQuery.ClientID + "\',\'" + this.lstQueryTokens.ClientID + "\');";
				}
				else
				{
					this.cmdInsertQueryToken.Visible = false;
				}
				
				strOperationSuccess = Localization.GetString("OperationSuccess", this.LocalResourceFile);
				strInvalidQuery = Localization.GetString("InvalidQuery", this.LocalResourceFile);
				//this.lblHeading.Text = Localization.GetString("Heading", this.LocalResourceFile);
				
				
				objFormSettings = Utility.GetFormSettings(ModuleId);
				
				if (!Page.IsPostBack)
				{
					BindData();
				}
				
				if ((!Request.UserAgent.Contains("MSIE")) || Request.Browser.Platform.Contains("Mac"))
				{
					if (DotNetNuke.Framework.AJAX.IsInstalled())
					{
						Utility.DisableAjax(this);
					}
				}
				
			}
			catch (Exception exc)
			{
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(_loaderCtl, exc.Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
				DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
			}
		}

		protected void cmdUpdate_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				if (this.TestQuery(false))
				{
					Code5Systems.FormMaster.Business.FormController objFormCtl = new Code5Systems.FormMaster.Business.FormController(ModuleId);
					Code5Systems.FormMaster.Business.FormInfo objFormInfo = objFormCtl.FormInfo;

					objFormInfo.ConnectionString = this.txtConnection.Text;
					objFormInfo.FormQuery = this.txtQuery.Text;

					objFormCtl.Update(objFormInfo);

					DotNetNuke.UI.Skins.Skin.AddModuleMessage(_loaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);

				}

			}
			catch (Exception ex)
			{
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(_loaderCtl, ex.Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
			}
		}

		protected void cmdTestQuery_Click(System.Object sender, System.EventArgs e)
		{

			this.TestQuery(true);

		}

#endregion
		
		
#region Data Bind Methods
		
		private void BindData()
		{
			
			try
			{
				Code5Systems.FormMaster.Business.FormController objFormCtl = new Code5Systems.FormMaster.Business.FormController(ModuleId);
				Code5Systems.FormMaster.Business.FormInfo objFormInfo = objFormCtl.FormInfo;
				
				this.txtConnection.Text = objFormInfo.ConnectionString;
				this.txtQuery.Text = objFormInfo.FormQuery;
				
				ProfilePropertyDefinitionCollection properties = objFormCtl.GetProfileProperties();
				
				this.lstQueryTokens.Items.Clear();
				foreach (ProfilePropertyDefinition profProperty in properties)
				{
					this.lstQueryTokens.Items.Add(new ListItem(profProperty.PropertyCategory + ": " + profProperty.PropertyName, "#" + profProperty.PropertyName + "#"));
				}
				
				this.lstQueryTokens.Items.Add(new ListItem(Localization.GetString("UserID", Utility.GetSharedResourceFile()), "#UserID#"));
				this.lstQueryTokens.Items.Add(new ListItem(Localization.GetString("UserName", Utility.GetSharedResourceFile()), "#UserName#"));
				this.lstQueryTokens.Items.Add(new ListItem(Localization.GetString("FullName", Utility.GetSharedResourceFile()), "#FullName#"));
				this.lstQueryTokens.Items.Add(new ListItem(Localization.GetString("DisplayName", Utility.GetSharedResourceFile()), "#DisplayName#"));
				this.lstQueryTokens.Items.Add(new ListItem(Localization.GetString("URLToken", Utility.GetSharedResourceFile()), "#URL:YOURURLPARAMNAME#"));
				this.lstQueryTokens.Items.Add(new ListItem(Localization.GetString("SessionToken", Utility.GetSharedResourceFile()), "#SESSION:YOURSESSIONPARAMNAME#"));
				
			}
			catch (Exception ex)
			{
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(_loaderCtl, ex.Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
			}
			
			
		}
		
#endregion
		
		
#region Public Properties
		public PortalModuleBase LoaderCtl
		{
			get
			{
				if (_loaderCtl == null)
				{
					_loaderCtl = this;
				}
				
				return _loaderCtl;
			}
			set
			{
				_loaderCtl = value;
			}
		}
#endregion

#region Private Methods

	private bool TestQuery(bool showResults)
	{
		bool bIsValid = false;
		string strQuery = default(string);
		strQuery = this.txtQuery.Text;
		try
		{
				
			if (string.IsNullOrEmpty(strQuery))
			{
				bIsValid = true;
			}
			else
			{
				Code5Systems.FormMaster.Business.FormController objFormCtl = new Code5Systems.FormMaster.Business.FormController(ModuleId);
				DataTable dt = objFormCtl.GetQueryData(this.txtConnection.Text, strQuery);
					
				if (dt.Rows.Count > 0)
				{
					this.gvQueryResults.DataSource = dt;
					this.gvQueryResults.DataBind();
					this.gvQueryResults.Visible = true;
					bIsValid = true;
				}
				else
				{
					DotNetNuke.UI.Skins.Skin.AddModuleMessage(_loaderCtl, Localization.GetString("NoResults", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
				}
					
				this.gvQueryResults.Visible = showResults;
			}
				
		}
		catch (Exception ex)
		{
			DotNetNuke.UI.Skins.Skin.AddModuleMessage(_loaderCtl, ex.Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
		}
			
		return bIsValid;
			
	}

#endregion Private Methods
		
	}
	
}


